// src/router/index.ts
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
import CV from '../views/CV.vue';
import ToDoList from '../views/ToDoList.vue';
import LoginView from '../views/LoginView.vue';
import { useUserStore } from '../stores/user';
import TestView from '../views/TestView.vue';
import CourseGrades from '../views/CourseGrades.vue';
import PersonalGrades from '../views/PersonalGrades.vue';
import type { RouteLocationNormalized, NavigationGuardNext } from 'vue-router';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'new-home',
      component: HomeView,
    },
    {
      path: '/cv',
      name: 'cv',
      component: CV,
    },
    {
      path: '/todolist',
      name: 'todolist',
      component: ToDoList,
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
    {
      path: '/test',
      name: 'test',
      component: TestView,
    },
    {
      path: '/coursegrades',
      name: 'coursegrades',
      component: CourseGrades,
    },
    {
      path: '/personalgrades',
      name: 'personalgrades',
      component: PersonalGrades,
    }
  ],
});

router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => { // 使用修正后的类型
  const userStore = useUserStore();
  if (to.name === 'todolist' && !userStore.isAuthenticated) {
    next({ name: 'login' });
  } else {
    next();
  }
});

export default router;